package com.yxdj.feedback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxdj.common.core.constant.SecurityConstants;
import com.yxdj.common.core.domain.R;
import com.yxdj.common.core.exception.ServiceException;
import com.yxdj.feedback.domain.ComplaintImage;
import com.yxdj.feedback.domain.UserComplaint;
import com.yxdj.feedback.mapper.ComplaintImageMapper;
import com.yxdj.feedback.mapper.UserComplaintMapper;
import com.yxdj.feedback.service.UserComplaintService;
import com.yxdj.order.api.RemoteOrderService;
import com.yxdj.order.api.domain.OrderInfo;
import com.yxdj.user.api.RemoteEngineerService;
import com.yxdj.user.api.RemoteManageUserService;
import com.yxdj.user.api.domain.Engineer;
import com.yxdj.user.api.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 韩总
 * @description 针对表【user_complaint(用户投诉表)】的数据库操作Service实现
 * @createDate 2025-03-27 16:49:37
 */
@Service
public class UserComplaintServiceImpl extends ServiceImpl<UserComplaintMapper, UserComplaint>
        implements UserComplaintService {

    @Autowired
    private RemoteOrderService remoteOrderService;

    @Autowired
    private RemoteEngineerService remoteEngineerService;

    @Autowired
    private RemoteManageUserService remoteManageUserService;

    @Autowired
    private ComplaintImageMapper complaintImageMapper;

    @Override
    public UserComplaint getUserComplainDetailById(Integer id) {
        UserComplaint userComplaint = baseMapper.selectById(id);
        //获取订单信息
        R<OrderInfo> orderInfo = remoteOrderService.getOrderInfoById(userComplaint.getOrderId(), SecurityConstants.INNER);
        if (R.FAIL == orderInfo.getCode()) {
            throw new ServiceException(orderInfo.getMsg());
        }
        //获取订单信息
        OrderInfo data = orderInfo.getData();
        //获取工程师id
        Long engineerId = data.getEngineerId();
        //获取工程师信息
        R<Engineer> engineer = remoteEngineerService.getEngineerById(engineerId, SecurityConstants.INNER);
        if (R.FAIL == engineer.getCode()) {
            throw new ServiceException(engineer.getMsg());
        }
        //设置工程师手机号
        userComplaint.setEngineerPhoneNumber(engineer.getData().getPhoneNumber());

        //获取用户信息
        R<User> user = remoteManageUserService.getUserById(userComplaint.getUserId(), SecurityConstants.INNER);
        if (R.FAIL == user.getCode()) {
            throw new ServiceException(user.getMsg());
        }
        userComplaint.setUserPhoneNumber(user.getData().getPhoneNumber());

        if (userComplaint.getHasImages() == 1) {
            List<ComplaintImage> complaintImages = complaintImageMapper.selectList(new LambdaQueryWrapper<ComplaintImage>().eq(ComplaintImage::getComplaintId, id));
            userComplaint.setComplaintImages(complaintImages);
        }
        return userComplaint;
    }
}




